package com.example.activitytest;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class FirstActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//        给当前活动加载一个布局：setContentView中传入布局文件的id
        setContentView(R.layout.first_layout);

        Button button1=(Button) findViewById(R.id.button_1);
        //setOnClickListener注册一个监听事件
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //makeText传入三个参数，第一个参数是context，也就是Tosat要求的上下文，第二个是文本，第三个是时长（Toast.LENGTH_SHORT/Toast.LENGTH_LONG两个参数可选）
                //Toast.makeText(FirstActivity.this,"点击了按钮1",Toast.LENGTH_SHORT).show();

                //显式Intent
               // Intent intent=new Intent(FirstActivity.this,SecondActivity.class);
               // startActivity(intent);

                //隐式Intent
                //Intent intent=new Intent("com.example.activitytest.ACTION_START");
                 //默认的categpry时可省略此句，也可以指定多个cagegory，此处只有一个com.example.activitytest.MY_CATEGORY
                //intent.addCategory("com.example.activitytest.MY_CATEGORY");
                //startActivity(intent);

                //隐式Intent打开浏览器,打开电话簿
                //Intent intent =new Intent(Intent.ACTION_VIEW);
                //intent.setData(Uri.parse("http://wwww.baidu.com"));
                //intent.setData(Uri.parse("tel:10086"));
                //startActivity(intent);

                //显式Intent向SencondActivity传递参数
                // String data="Hello SecondActivity";
                // Intent intent=new Intent(FirstActivity.this,SecondActivity.class);
                // intent.putExtra("extra_data",data);
                // startActivity(intent);

                //有数据接收的启动SecondActivity
                Intent intent =new Intent(FirstActivity.this,SecondActivity.class);
                startActivityForResult(intent,1);

            }
        });
    }

    //接收SecondActivity的回调
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        switch (requestCode){
            case 1:
                if(resultCode==RESULT_OK){
                    String returnedData=data.getStringExtra("data_return");
                    Log.d("FirstActivity",returnedData);
                }
        }
    }

    //给当前活动创建菜单
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main,menu);
        return true;
    }

    //菜单增加监听事件
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.add_item:
                Toast.makeText(this,"点击添加",Toast.LENGTH_SHORT).show();
                break;
            case R.id.remove_item:
                Toast.makeText(this,"点击删除",Toast.LENGTH_SHORT).show();
                break;
                default:
        }
        return true;
    }
}
